package com.ymatou.iapi.liveseller.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveseller.parameter.RemoveLiveProductByPidBean;
import com.ymatou.iapi.liveseller.service.RemoveLiveProductByPidCall;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.TestDataManager;
/**
*批量从直播中移除商品Id
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_RemoveLiveProductByPid{
	private static RemoveLiveProductByPidBean removeliveproductbypidBean;
	private static RemoveLiveProductByPidCall removeliveproductbypidCall;
	private static LiveRoomWapper liveRoomWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量从直播中移除商品Id");
	}

	@Before
	public void caseUp() {
		removeliveproductbypidBean=new RemoveLiveProductByPidBean();
		removeliveproductbypidCall=new RemoveLiveProductByPidCall();
		liveRoomWapper=new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(removeliveproductbypidCall.getOpurl());
		liveRoomWapper=null;
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(Integer liveid, String pid, Integer uid) throws Exception {
		List<Map> listm = liveRoomWapper.selectLiveproductByIdAndPid(liveid, pid);
		Logger.verifyEquals(1, listm.size(), "验证商品记录加入到数据库");
		Map pinfo = listm.get(0);
		Logger.verifyEquals(uid, pinfo.get("UserId"), "验证用户id");
		Logger.verifyEquals(-1, pinfo.get("Action"), "验证Action删除");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RemoveLiveProductByPid_001() {
		Logger.start(true,"批量从直播中移除商品Id成功");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			List<String> ProductIdList = new ArrayList<>();
			int count = 1;
			for (int i = 0; i < count; i++) {
				String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
				
				Logger.comment("创建商品：" + pid);
				ProductIdList.add(pid);
			}
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), ProductIdList);
			removeliveproductbypidBean.setUserId(createlive.getUserId());
			removeliveproductbypidBean.setProductIdList(ProductIdList);
			removeliveproductbypidCall.setData(removeliveproductbypidBean);
			removeliveproductbypidCall.callService();			
			Logger.verifyEquals("200", removeliveproductbypidCall.getString("Code"), "验证Code");
			for (String pid : ProductIdList) {
				verifyDB(createlive.getId(),pid, createlive.getUserId());
			}
			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(0, liveroom.getPutawayCount(), "验证PutawayCount");
			Logger.verifyEquals(0, liveroom.getLiveProductNum(), "验证LiveProductNum");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_RemoveLiveProductByPid_002() {
		Logger.start(true,"不输入userid失败");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			List<String> ProductIdList = new ArrayList<>();
			int count = 2;
			for (int i = 0; i < count; i++) {
				String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
				
				Logger.comment("创建商品：" + pid);
				ProductIdList.add(pid);
			}
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), ProductIdList);
//			removeliveproductbypidBean.setUserId(createlive.getUserId());
			removeliveproductbypidBean.setProductIdList(ProductIdList);
			removeliveproductbypidCall.setData(removeliveproductbypidBean);
			removeliveproductbypidCall.callService();			
			Logger.verifyEquals("400", removeliveproductbypidCall.getString("Code"), "验证Code");
			Logger.verifyIsContains("不能为空", removeliveproductbypidCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_RemoveLiveProductByPid_003() {
		Logger.start(true,"不输入ProductIdList失败");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
//			List<String> ProductIdList = new ArrayList<>();
//			int count = 2;
//			for (int i = 0; i < count; i++) {
//				String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
//				
//				Logger.comment("创建商品：" + pid);
//				ProductIdList.add(pid);
//			}
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
//			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), ProductIdList);
			removeliveproductbypidBean.setUserId(createlive.getUserId());
//			removeliveproductbypidBean.setProductIdList(ProductIdList);
			removeliveproductbypidCall.setData(removeliveproductbypidBean);
			removeliveproductbypidCall.callService();			
			Logger.verifyEquals("201", removeliveproductbypidCall.getString("Code"), "验证Code");
			Logger.verifyEquals("参数错误，移除商品失败", removeliveproductbypidCall.getString("Msg"), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_RemoveLiveProductByPid_004() {
		Logger.start(true,"批量从直播中移除商品多个Id成功");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			List<String> ProductIdList = new ArrayList<>();
			int count = 2;
			for (int i = 0; i < count; i++) {
				String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
				
				Logger.comment("创建商品：" + pid);
				ProductIdList.add(pid);
			}
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), ProductIdList);
			removeliveproductbypidBean.setUserId(createlive.getUserId());
			removeliveproductbypidBean.setProductIdList(ProductIdList);
			removeliveproductbypidCall.setData(removeliveproductbypidBean);
			removeliveproductbypidCall.callService();			
			Logger.verifyEquals("200", removeliveproductbypidCall.getString("Code"), "验证Code");
			for (String pid : ProductIdList) {
				verifyDB(createlive.getId(),pid, createlive.getUserId());
			}
			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(0, liveroom.getPutawayCount(), "验证PutawayCount");
			Logger.verifyEquals(0, liveroom.getLiveProductNum(), "验证LiveProductNum");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_RemoveLiveProductByPid_005() {
		Logger.start(true,"商品和用户不匹配移除不操作");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			List<String> ProductIdList = new ArrayList<>();
			int count = 1;
			for (int i = 0; i < count; i++) {
				String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
				
				Logger.comment("创建商品：" + pid);
				ProductIdList.add(pid);
			}
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), ProductIdList);
			removeliveproductbypidBean.setUserId(createlive.getUserId()-1);
			removeliveproductbypidBean.setProductIdList(ProductIdList);
			removeliveproductbypidCall.setData(removeliveproductbypidBean);
			removeliveproductbypidCall.callService();			
			Logger.verifyEquals("200", removeliveproductbypidCall.getString("Code"), "验证Code");
			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(1, liveroom.getPutawayCount(), "验证PutawayCount");
			Logger.verifyEquals(1, liveroom.getLiveProductNum(), "验证LiveProductNum");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_RemoveLiveProductByPid_006() {
		Logger.start(true,"操作多个商品,其中有一个无效");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			List<String> ProductIdList = new ArrayList<>();
			//创建3个
			int count = 3;
			for (int i = 0; i < count; i++) {
				String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
				
				Logger.comment("创建商品：" + pid);
				ProductIdList.add(pid);
			}
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), ProductIdList);
			//移除2个 再加一个无效商品
			ProductIdList.remove(0);
			ProductIdList.add(TestDataManager.getRandomUUID().toString());
			
			removeliveproductbypidBean.setUserId(createlive.getUserId());
			removeliveproductbypidBean.setProductIdList(ProductIdList);
			removeliveproductbypidCall.setData(removeliveproductbypidBean);
			removeliveproductbypidCall.callService();			
			Logger.verifyEquals("200", removeliveproductbypidCall.getString("Code"), "验证Code");
			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(1, liveroom.getPutawayCount(), "验证PutawayCount");
			Logger.verifyEquals(1, liveroom.getLiveProductNum(), "验证LiveProductNum");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}